package com.sojson.controller.po;

import java.io.IOException;
import java.math.BigDecimal;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.sojson.config.exception.TransErrorCode;
import com.sojson.config.exception.TransException;
import com.sojson.constant.Constant;
import com.sojson.controller.po.param.verify.MemberParamVerify;
import com.sojson.dao.MemberDao;
import com.sojson.entity.bo.AuthcUser;
import com.sojson.entity.bo.Select;
import com.sojson.entity.po.Member;
import com.sojson.entity.vo.HotelOrderVo;
import com.sojson.entity.vo.MemberVo;
import com.sojson.entity.vo.OrderVo;
import com.sojson.entity.vo.PostApplyVo;
import com.sojson.entity.vo.UserCouponVo;
import com.sojson.enums.EUserAuthStatus;
import com.sojson.enums.core.EGeneralStatus;
import com.sojson.enums.core.ERoleType;
import com.sojson.enums.core.EUserCouponStatus;
import com.sojson.result.Result;
import com.sojson.result.bean.Status;
import com.sojson.service.HotelOrderService;
import com.sojson.service.MemberService;
import com.sojson.service.OrderService;
import com.sojson.service.PostApplyService;
import com.sojson.service.UserCouponService;
import com.sojson.util.ArithUtil;
import com.sojson.util.StringUtil;
import com.sojson.util.file.FileUtil;
import com.sojson.util.idcard.IdCardUtil;
import com.sojson.util.pay.wx.WxUtil;
import com.sojson.util.pwd.PwdUtil;
import com.sojson.util.token.TokenUtil;
import com.sojson.util.token.TokenVo;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 用户表控制器
 * 
 * @author liu
 * @date 2021-03-16
 */
@Api(tags = "用户表管理")
@RestController
@RequestMapping(value = "/member")
public class MemberController {

    @Autowired
    private PostApplyService postApplyService;
    @Autowired
    private OrderService orderService;
    @Autowired
    private HotelOrderService hotelOrderService;
    @Autowired
    private UserCouponService userCouponService;
    @Autowired
    private MemberService memberServiceImpl;
    @Resource
    private MemberDao memberDaoImpl;

    /**
     * 单条插入
     * 
     * @param member
     * @return
     * @throws SQLException
     */
    // @RequestMapping(value = "/insert", method = RequestMethod.POST)
    public Object insert(@RequestBody Member member) {
        memberServiceImpl.insert(member);
        return Result.success();
    }

    /**
     * 批量插入
     * 
     * @param member
     * @return
     * @throws SQLException
     */
    // @RequestMapping(value = "/insertList", method = RequestMethod.POST)
    public Object insertList(@RequestBody Member member) {
        return Result.success(memberServiceImpl.insertList(member));
    }

    /**
     * 通过对象批量删除(物理删除)
     * 
     * @param member
     * @return
     * @throws SQLException
     */
    // @RequestMapping(value = "/deleteByDto", method = RequestMethod.POST)
    public Object deleteByDto(@RequestBody Member member) {
        return Result.success(memberServiceImpl.deleteByDto(member));
    }

    /**
     * 统计全部数量
     * 
     * @return
     * @throws SQLException
     */
    // @RequestMapping(value = "/countAll", method = RequestMethod.GET)
    public Object countAll() {
        int countAll = memberServiceImpl.countAll();
        return Result.success(countAll);
    }

    /**
     * 查询所有
     * 
     * @return
     * @throws SQLException
     */
    // @RequestMapping(value = "/findAll", method = RequestMethod.GET)
    public Object findAll() {
        List<Member> findAll = memberServiceImpl.findAll();
        return Result.success(findAll);
    }

    /**
     * 根据对象统计数量
     * 
     * @param member
     * @return
     * @throws SQLException
     */
    // @RequestMapping(value = "/countByDto", method = RequestMethod.POST)
    public Object countByDto(@RequestBody Member member) {
        int countByDto = memberServiceImpl.countByDto(member);
        return Result.success(countByDto);
    }

    /**
     * 根据对象查询
     * 
     * @param member
     * @return
     * @throws SQLException
     */
    // @RequestMapping(value = "/findByDto", method = RequestMethod.POST)
    public Object findByDto(@RequestBody Member member) {
        List<Member> findByDto = memberServiceImpl.findByDto(member);
        return Result.success(findByDto);
    }

    /**
     * 通过id删除(物理删除)
     * 
     * @param id
     * @return
     * @throws SQLException
     */
    // @RequestMapping(value = "/deleteById", method = RequestMethod.POST)
    public Object deleteById(Integer id) {
        memberServiceImpl.deleteById(id);
        return Result.success();
    }

    /**
     * 通过id列表删除(物理删除)
     * 
     * @param ids
     * @return
     * @throws SQLException
     */
    // @RequestMapping(value = "/deleteByIdList", method = RequestMethod.POST)
    public Object deleteByIdList(@RequestParam("ids[]") ArrayList<Integer> ids) {
        return Result.success(memberServiceImpl.deleteByIdList(ids));
    }

    /**
     * 通过id更新(全部更新)
     * 
     * @param member
     * @return
     * @throws SQLException
     */
    // @RequestMapping(value = "/updateById", method = RequestMethod.POST)
    public Object updateById(@RequestBody Member member) {
        memberServiceImpl.updateById(member);
        return Result.success();
    }

    /**
     * 通过id列表更新(全部更新)(更新内容相同)
     * 
     * @param member
     * @return
     * @throws SQLException
     */
    // @RequestMapping(value = "/updateStaticByIdList", method = RequestMethod.POST)
    public Object updateStaticByIdList(@RequestBody Member member) {
        return Result.success(memberServiceImpl.updateStaticByIdList(member));
    }

    /**
     * 通过id列表更新(全部更新)(更新内容不相同)
     * 
     * @param member
     * @return
     * @throws SQLException
     */
    // @RequestMapping(value = "/updateChangeByIdList", method = RequestMethod.POST)
    public Object updateChangeByIdList(@RequestBody Member member) {
        return Result.success(memberServiceImpl.updateChangeByIdList(member));
    }

    /**
     * 通过id更新(局部更新)
     * 
     * @param member
     * @return
     * @throws SQLException
     * @throws IOException 
     */
    @PutMapping(value = "/updatePartialById")
    @PreAuthorize("@ss.login()")
    public Object updatePartialById(@RequestBody Member member) throws SQLException, IOException {
        // 校验参数
        Result<?> paramResult = MemberParamVerify.updatePartialById(member);
        // 验证不通过
        if (!paramResult.getCode().equals(TransErrorCode.SUCCESS.getCode())) {
            return paramResult;
        }

        // 参数初始化
        member.setId(TokenUtil.getUserId());
        member.updateInit();
        member.setUpdateNull();
        // 修改信息
        memberServiceImpl.updatePartialById(member);
        return Result.success();
    }

    /**
     * 通过id更新(局部更新)
     * 
     * @param member
     * @return
     * @throws Exception 
     */
    @PutMapping(value = "/updatePartialWebById")
    @PreAuthorize("@ss.hasPermi('/member/update')")
    public Object updatePartialWebById(@RequestBody Member member) throws Exception {
        // 校验参数
        Result<?> paramResult = MemberParamVerify.updatePartialById(member);
        // 验证不通过
        if (!paramResult.getCode().equals(TransErrorCode.SUCCESS.getCode())) {
            return paramResult;
        }

        // 参数初始化
        member.updateInit();
        member.setUpdateNullWeb();
        String pwd = member.getPwd();
        if (StringUtil.isNotBlankObject(pwd)) {
            member.encryptionPwd();
        }
        String payPwd = member.getPayPwd();
        if (StringUtil.isNotBlankObject(payPwd)) {
            member.setPayPwd(PwdUtil.encryptionPwd(payPwd));
        }
        // 修改信息
        memberServiceImpl.updatePartialById(member);
        return Result.success();
    }

    /**
     * 通过id列表更新(局部更新)(更新内容相同)
     * 
     * @param member
     * @return
     * @throws SQLException
     */
    // @RequestMapping(value = "/updateStaticPartialByIdList", method = RequestMethod.POST)
    public Object updateStaticPartialByIdList(@RequestBody Member member) {
        return Result.success(memberServiceImpl.updateStaticPartialByIdList(member));
    }

    /**
     * 通过id列表更新(局部更新)(更新内容不相同)
     * 
     * @param member
     * @return
     * @throws SQLException
     */
    // @RequestMapping(value = "/updateChangePartialByIdList", method = RequestMethod.POST)
    public Object updateChangePartialByIdList(@RequestBody Member member) {
        return Result.success(memberServiceImpl.updateChangePartialByIdList(member));
    }

    /**
     * 通过id查询
     * 
     * @param id
     * @return
     */
    @RequestMapping(value = "/findById", method = RequestMethod.GET)
    public Object findById(Integer id) {
        Member findById = memberServiceImpl.findById(id);
        return Result.success(findById);
    }

    /**
     * 通过编号查询
     * 
     * @param no
     * @return
     */
    @RequestMapping(value = "/findByNo", method = RequestMethod.GET)
    public Object findById(String no) {
        Member findByNo = memberServiceImpl.findByNo(no);
        return Result.success(findByNo);
    }

    /**
     * 通过id列表查询
     * 
     * @param ids
     * @return
     * @throws SQLException
     */
    // @RequestMapping(value = "/findByIdList", method = RequestMethod.GET)
    public Object findByIdList(@RequestParam("ids[]") ArrayList<Integer> ids) {
        ArrayList<Member> findByIdList = memberServiceImpl.findByIdList(ids);
        return Result.success(findByIdList);
    }

    /**
     * 通过对象查询id列表
     * 
     * @param member
     * @return
     * @throws SQLException
     */
    // @RequestMapping(value = "/findIdByDto", method = RequestMethod.POST)
    public Object findIdByDto(@RequestBody Member member) {
        ArrayList<Integer> findIdByDto = memberServiceImpl.findIdByDto(member);
        return Result.success(findIdByDto);
    }

    /**
     * 清空数据(物理清空)
     * 
     * @return
     * @throws SQLException
     */
    // @RequestMapping(value = "/clear", method = RequestMethod.POST)
    public Object clear() {
        memberServiceImpl.clear();
        return Result.success();
    }

    /**
     * 通过id更新手机号
     * 
     * @param member
     * @return
     * @throws Exception 
     */
    @PutMapping(value = "/updateMobileById")
    @PreAuthorize("@ss.login()")
    public Object updateMobileById(@RequestBody TokenVo member) throws Exception {
        // 校验参数
        Result<?> paramResult = MemberParamVerify.updateMobileById(member);
        // 验证不通过
        if (!paramResult.getCode().equals(TransErrorCode.SUCCESS.getCode())) {
            return paramResult;
        }

        // 修改手机号
        memberServiceImpl.updateMobileById(member);
        return Result.success();
    }

    /**
     * 通过id验证旧密码修改密码
     * 
     * @param member
     * @return
     * @throws Exception 
     */
    @PutMapping(value = "/updatePwdByOldPwd")
    @PreAuthorize("@ss.login()")
    public Object updatePwdByOldPwd(@RequestBody AuthcUser member) throws Exception {
        // 校验参数
        Result<?> paramResult = MemberParamVerify.updatePwdByOldPwd(member);
        // 验证不通过
        if (!paramResult.getCode().equals(TransErrorCode.SUCCESS.getCode())) {
            return paramResult;
        }

        member.setId(TokenUtil.getUserId());
        // 修改密码
        memberServiceImpl.updatePwdByOldPwd(member);
        return Result.success();
    }

    /**
     * 通过id验证旧密码修改密码
     * 
     * @param member
     * @return
     * @throws Exception 
     */
    @PutMapping(value = "/updatePwdById")
    @PreAuthorize("@ss.hasPermi('/member/updatePwdById')")
    public Object updatePwdById(@RequestBody AuthcUser member) throws Exception {
        // 校验参数
        Result<?> paramResult = MemberParamVerify.updatePwdById(member);
        // 验证不通过
        if (!paramResult.getCode().equals(TransErrorCode.SUCCESS.getCode())) {
            return paramResult;
        }

        Member m = new Member();
        m.updateInit();
        m.setId(member.getId());
        m.setPwd(PwdUtil.encryptionPwd(member.getPwd()));
        // 修改密码
        return Result.success(memberServiceImpl.updatePartialById(m));
    }

    /**
     * 通过id验证短信修改密码
     * 
     * @param token
     * @return
     * @throws Exception 
     */
    @PutMapping(value = "/updatePwdBySms")
    @PreAuthorize("@ss.login()")
    public Object updatePwdBySms(@RequestBody TokenVo token) throws Exception {
        // 校验参数
        Result<?> paramResult = MemberParamVerify.updatePwdBySms(token);
        // 验证不通过
        if (!paramResult.getCode().equals(TransErrorCode.SUCCESS.getCode())) {
            return paramResult;
        }

        // 修改密码
        memberServiceImpl.updatePwdBySms(token);
        return Result.success();
    }

    /**
     * 通过id修改状态
     * 
     * @param token
     * @return
     * @throws Exception 
     */
    @PutMapping(value = "/updateStatusById")
    @PreAuthorize("@ss.hasPermi('/member/updateStatusById')")
    public Object updateStatusById(@RequestBody Member member) throws Exception {
        // 校验参数
        Result<?> paramResult = MemberParamVerify.updateStatusById(member);
        // 验证不通过
        if (!paramResult.getCode().equals(TransErrorCode.SUCCESS.getCode())) {
            return paramResult;
        }

        // 修改状态
        memberServiceImpl.updateStatusById(member);
        return Result.success();
    }

    /**
     * 获取当前Token字符串
     * 
     * @return
     * @throws Exception
     */
    @GetMapping("/getClaimsStr")
    public Object getClaimsStr(@RequestParam(defaultValue = "true") Boolean isCreate) throws Exception {
        return Result.success(TokenUtil.getClaimsStr(isCreate));
    }

    /**
     * 获取当前登录用户
     * 
     * @return
     * @throws IOException 
     */
    @GetMapping("/getOnline")
    @PreAuthorize("@ss.login()")
    public Object getOnline(Member member) throws IOException {
        return Result.success(TokenUtil.getToken());
    }

    /**
     * 获取当前最新的登录用户
     * 
     * @return
     * @throws IOException 
     */
    @GetMapping("/getOnlineNew")
    @PreAuthorize("@ss.login()")
    public Object getOnlineNew(Member member) throws IOException {
        return Result.success(TokenUtil.refreshTokenUser());
    }

    /**
     * 修改认证信息
     * 
     * @param member
     * @return
     * @throws Exception 
     * @throws SQLException
     */
    @PutMapping(value = "/updateAuth")
    @PreAuthorize("@ss.login()")
    public Object updateAuth(@RequestBody Member member) throws Exception {
        // 校验参数
        Result<?> paramResult = MemberParamVerify.updateAuth(member);
        // 验证不通过
        if (!paramResult.getCode().equals(TransErrorCode.SUCCESS.getCode())) {
            return paramResult;
        }

        Member mem = new Member();
        // 参数初始化
        mem.updateInit();
        mem.setId(member.getId());
        mem.setName(member.getName());
        mem.setIdCard(member.getIdCard());
        mem.setIdCardFront(member.getIdCardFront());
        mem.setIdCardVerso(member.getIdCardVerso());
        mem.setAuthStatus(EUserAuthStatus.YES.getCode());

        Status nameAndIdCard = IdCardUtil.nameAndIdCard(mem.getName(), mem.getIdCard());
        if (nameAndIdCard.isFail()) {
            return Result.fail("姓名身份证不匹配！");
        }
        mem.setAge(IdCardUtil.getAge(mem.getIdCard()));
        // 修改信息
        memberServiceImpl.updatePartialById(mem);
        return Result.success();
    }

    /**
     * 审核认证信息
     * 
     * @param member
     * @return
     * @throws SQLException
     */
    @PutMapping(value = "/auditAuth")
    @PreAuthorize("@ss.hasPermi('/member/auditAuth')")
    public Object auditAuth(@RequestBody Member member) {
        // 校验参数
        Result<?> paramResult = MemberParamVerify.auditAuth(member);
        // 验证不通过
        if (!paramResult.getCode().equals(TransErrorCode.SUCCESS.getCode())) {
            return paramResult;
        }

        Member mem = new Member();
        // 参数初始化
        mem.updateInit();
        mem.setId(member.getId());
        mem.setAuthStatus(member.getAuthStatus());
        // 修改信息
        memberServiceImpl.auditAuth(mem);
        return Result.success();
    }

    /**
     * 修改支付密码
     * 
     * @param member
     * @return
     * @throws SQLException
     * @throws IOException 
     */
    @PutMapping(value = "/updatePayPwd")
    @PreAuthorize("@ss.login()")
    public Object updatePayPwd(@RequestBody AuthcUser member) throws SQLException, IOException {
        // 校验参数
        Result<?> paramResult = MemberParamVerify.updatePayPwd(member);
        // 验证不通过
        if (!paramResult.getCode().equals(TransErrorCode.SUCCESS.getCode())) {
            return paramResult;
        }

        // 参数初始化
        AuthcUser mem = new AuthcUser();
        mem.updateInit();
        mem.setId(TokenUtil.getUserId());
        String oldPayPwd = member.getOldPayPwd();
        if (StringUtil.isNotBlankObject(oldPayPwd)) {
            mem.setOldPayPwd(oldPayPwd);
        }
        mem.setPayPwd(PwdUtil.encryptionPwd(member.getPayPwd()));
        // 修改信息
        return Result.success(memberServiceImpl.updatePayPwd(mem));
    }

    /**
     * 修改支付密码
     * 
     * @param member
     * @return
     * @throws SQLException
     */
    @PutMapping(value = "/updatePayPwdWeb")
    @PreAuthorize("@ss.hasPermi('/member/updatePayPwdWeb')")
    public Object updatePayPwdWeb(@RequestBody AuthcUser member) {
        // 校验参数
        Result<?> paramResult = MemberParamVerify.updatePayPwdWeb(member);
        // 验证不通过
        if (!paramResult.getCode().equals(TransErrorCode.SUCCESS.getCode())) {
            return paramResult;
        }

        // 参数初始化
        AuthcUser mem = new AuthcUser();
        mem.updateInit();
        mem.setId(member.getId());
        mem.setPayPwd(PwdUtil.encryptionPwd(member.getPayPwd()));
        // 修改信息
        return Result.success(memberServiceImpl.updatePartialById(mem));
    }

    /**
     * 获取当前佣金信息
     * 
     * @param id
     * @return
     * @throws SQLException
     * @throws IOException 
     */
    @GetMapping(value = "/findBalance")
    @PreAuthorize("@ss.hasPermi('/member/findBalance')")
    public Object findBalance(Integer id) {
        return Result.success(memberServiceImpl.findBalance(id));
    }

    /**
     * 获取当前余额信息
     * 
     * @param id
     * @return
     * @throws SQLException
     * @throws IOException 
     */
    @GetMapping(value = "/findBrokerage")
    @PreAuthorize("@ss.hasPermi('/member/findBrokerage')")
    public Object findBrokerage(Integer id) {
        return Result.success(memberServiceImpl.findBrokerage(id));
    }

    /**
     * 获取当前佣金信息
     * 
     * @return
     * @throws SQLException
     * @throws IOException 
     */
    @GetMapping(value = "/findAppBalance")
    @PreAuthorize("@ss.login()")
    public Object findAppBalance() throws SQLException, IOException {
        return Result.success(memberServiceImpl.findBalance(TokenUtil.getUserId()));
    }

    /**
     * 获取当前余额信息
     * 
     * @return
     * @throws SQLException
     * @throws IOException 
     */
    @GetMapping(value = "/findAppBrokerage")
    @PreAuthorize("@ss.login()")
    public Object findAppBrokerage() throws SQLException, IOException {
        return Result.success(memberServiceImpl.findBrokerage(TokenUtil.getUserId()));
    }

    /**
     * 获取下级用户列表
     * 
     * @param member
     * @return
     * @throws SQLException
     */
    @GetMapping("/findUnderlingByDto")
    @PreAuthorize("@ss.hasPermi('/member/findUnderlingByDto')")
    public Object findUnderlingByDto(Member member) {
        List<Member> findByDto = memberServiceImpl.findUnderlingByDto(member);
        return Result.success(findByDto, findByDto);
    }

    /**
     * 获取下级用户列表
     * 
     * @param member
     * @return
     * @throws SQLException
     * @throws IOException 
     */
    @GetMapping("/findXcxUnderlingByDto")
    @PreAuthorize("@ss.login()")
    public Object findXcxUnderlingByDto(Member member) throws SQLException, IOException {
        member.setTopId(TokenUtil.getUserId());
        List<Member> findByDto = memberServiceImpl.findUnderlingByDto(member);
        return Result.success(findByDto, findByDto);
    }

    /**
     * 获取简历
     * 
     * @param member
     * @return
     * @throws SQLException
     * @throws IOException 
     */
    @GetMapping("/findResumeList")
    @PreAuthorize("@ss.hasPermi('/member/findResumeList')")
    public Object findResumeList(Member member) throws SQLException, IOException {
        List<MemberVo> findByDto = memberServiceImpl.findResumeList(member);
        return Result.success(findByDto, findByDto);
    }

    /**
     * 获取简历
     * 
     * @param member
     * @return
     * @throws SQLException
     * @throws IOException 
     */
    @GetMapping("/findResume")
    @PreAuthorize("@ss.hasPermi('/member/findResume')")
    public Object findResume(Integer id) throws SQLException, IOException {
        MemberVo findAppResume = memberServiceImpl.findResume(id);
        return Result.success(findAppResume);
    }

    /**
     * 获取简历
     * 
     * @param member
     * @return
     * @throws SQLException
     * @throws IOException 
     */
    @GetMapping("/findAppResume")
    @PreAuthorize("@ss.login()")
    public Object findAppResume() throws SQLException, IOException {
        MemberVo findAppResume = memberServiceImpl.findResume(TokenUtil.getUserId());
        return Result.success(findAppResume);
    }

    /**
     * 获取简历
     * 
     * @param member
     * @return
     * @throws SQLException
     * @throws IOException 
     */
    @GetMapping("/findAppResumeList")
    @PreAuthorize("@ss.hasPermi('/member/findAppResumeList')")
    public Object findAppResumeList(Member member) throws SQLException, IOException {
        List<MemberVo> findByDto = memberServiceImpl.findResumeList(member);
        return Result.success(findByDto, findByDto);
    }

    /**
     * 根据对象查询
     * 
     * @param member
     * @return
     * @throws SQLException
     */
    @GetMapping("/list")
    @PreAuthorize("@ss.hasPermi('/member/list')")
    public Result<List<MemberVo>> list(Member member) {
        List<MemberVo> list = memberServiceImpl.list(member);
        return Result.success(list);
    }

    /**
     * 根据ID查询
     * 
     * @param id
     * @return
     * @throws SQLException
     */
    @GetMapping("/getInfo")
    @PreAuthorize("@ss.hasPermi('/member/getInfo')")
    public Result<MemberVo> getInfo(Integer id) {
        MemberVo getInfo = memberServiceImpl.getInfo(id);
        return Result.success(getInfo);
    }

    /**
     * 将佣金转入余额
     * 
     * @param balanceLog
     * @return
     * @throws IOException 
     */
    @PutMapping(value = "/balanceToBrokerage/{total}")
    @PreAuthorize("@ss.login()")
    public Result<Integer> balanceToBrokerage(@PathVariable BigDecimal total) throws IOException {
        int intValue = total.intValue();
        if (intValue <= 0) {
            throw new TransException("诚意金必须大于0！");
        }
        return Result.success(memberServiceImpl.balanceToBrokerage(ArithUtil.intPennyToBigDollar(intValue)));
    }

    /**
     * 将佣金提现到微信
     * 
     * @param balanceLog
     * @return
     * @throws IOException 
     */
    @PutMapping(value = "/balanceToWx/{total}")
    @PreAuthorize("@ss.login()")
    public Result<Integer> balanceToWx(@PathVariable BigDecimal total) throws IOException {
        int intValue = total.intValue();
        if (intValue <= 0) {
            throw new TransException("诚意金必须大于0！");
        }
        throw new TransException("商户号必须入驻满90天且连续交易30天才可开通转账权限！");
        // return Result.success();
    }

    /**
     * 将余额提现到微信
     * 
     * @param balanceLog
     * @return
     * @throws IOException 
     */
    @PutMapping(value = "/brokerageToWx/{total}")
    @PreAuthorize("@ss.login()")
    public Result<Integer> brokerageToWx(@PathVariable BigDecimal total) throws IOException {
        int intValue = total.intValue();
        if (intValue <= 0) {
            throw new TransException("诚意金必须大于0！");
        }
        throw new TransException("商户号必须入驻满90天且连续交易30天才可开通转账权限！");
        // return Result.success();
    }

    /**
     * 根据对象查询
     * 
     * @param member
     * @return
     */
    @GetMapping("/listByRole")
    @PreAuthorize("@ss.hasPermi('/member/listByRole')")
    public Result<List<MemberVo>> listMferchant(MemberVo member) {
        List<MemberVo> list = memberServiceImpl.listByRole(member);
        return Result.success(list);
    }

    /**
     * 根据对象查询
     * 
     * @param member
     * @return
     */
    @GetMapping("/listMferchantApp")
    public Result<List<MemberVo>> listMferchantApp(MemberVo member) {
        member.setRoleKey(ERoleType.MERCHANT.getKey());
        member.setStatus(EGeneralStatus.NORMAL.getCode());
        List<MemberVo> list = memberServiceImpl.listByRole(member);
        for (MemberVo memberVo : list) {
            String dist = memberVo.getDist();
            if (StringUtil.isNotBlankObject(dist)) {
                int indexOf = dist.indexOf(".");
                if (indexOf > 0) {
                    memberVo.setDist(dist.substring(0, indexOf));
                }
            }
        }
        return Result.success(list);
    }

    /**
     * 根据对象查询
     * 
     * @param member
     * @return
     */
    @GetMapping("/listUser")
    public Result<List<MemberVo>> listUser(MemberVo member) {
        member.setStatus(EGeneralStatus.NORMAL.getCode());
        List<MemberVo> list = memberServiceImpl.listUser(member);
        return Result.success(list);
    }

    /**
     * 生成小程序码
     * 
     * @return
     * @throws Exception 
     * @throws IOException 
     */
    @PostMapping(value = "/addXcxCodeImg")
    @PreAuthorize("@ss.login()")
    public Object addXcxCodeImg() throws IOException, Exception {
        byte[] wxAcodeunLimit = WxUtil.getWxAcodeunLimit(TokenUtil.getUser().getInvitationCode());
        String uploadingToThisLocalityDirectory = FileUtil.uploadToThisLocalityDirectory(wxAcodeunLimit,
            TokenUtil.getUserNo() + "." + Constant.GENERATE_IMG_SUFFIX, "/xcxImg");
        return Result.success(uploadingToThisLocalityDirectory);
    }

    /**
     * 获取下拉列表
     * 
     * @param member
     * @return
     */
    @GetMapping("/select")
    @ApiOperation("获取用户下拉列表")
    public Result<List<Select>> select(MemberVo member) {
        List<Select> list = memberServiceImpl.select(member);
        for (Select select : list) {
            select.init();
        }
        return Result.success(list);
    }

    /**
     * 获取下拉列表
     * 
     * @param member
     * @return
     */
    @GetMapping("/selectApp")
    @ApiOperation("获取用户下拉列表")
    public Result<List<Select>> selectApp(MemberVo member) {
        member.setStatus(EGeneralStatus.NORMAL.getCode());
        List<Select> list = memberServiceImpl.select(member);
        for (Select select : list) {
            select.init();
        }
        return Result.success(list);
    }

    /**
     * 获取商家下拉列表
     * 
     * @param member
     * @return
     */
    @GetMapping("/selectMerchant")
    @ApiOperation("获取用户下拉列表")
    public Result<List<Select>> selectMerchant(MemberVo member) {
        member.setRoleKey(ERoleType.MERCHANT.getKey());
        List<Select> list = memberServiceImpl.selectMerchant(member);
        for (Select select : list) {
            select.init();
        }
        return Result.success(list);
    }

    /**
     * 用户首页统计
     * 
     * @return
     */
    @GetMapping("/countApp")
    @PreAuthorize("@ss.login()")
    public Object countAllTypeApp() throws Exception {
        Integer userId = TokenUtil.getUserId();
        String userNo = TokenUtil.getUserNo();
        PostApplyVo postApply = new PostApplyVo();
        postApply.setUserId(userId);
        OrderVo order = new OrderVo();
        order.setUserNo(userNo);
        HotelOrderVo hotelOrder = new HotelOrderVo();
        hotelOrder.setNoUser(userNo);
        UserCouponVo userCoupon = new UserCouponVo();
        userCoupon.setUserNo(userNo);
        userCoupon.setStatus(EUserCouponStatus.USE_NO.getCode());

        // hireIng 待录用
        // hireNo 未录用
        // postsIng 待上岗
        // payIng 待结薪
        // paymentNo 待付款
        // sendOutNo 待发货
        // sendOutYes 待收货
        // commentNo 待评价
        // paymentNoHotel 待付款
        // checkNoHotel 待入住
        // checkYesHotel 已入住
        // commentNoHotel 待评价
        // useNo 优惠券数量
        Map<String, Integer> count = new HashMap<>(5);
        count.putAll(postApplyService.countUser(postApply));
        count.putAll(orderService.countUser(order));
        count.putAll(hotelOrderService.countUser(hotelOrder));
        count.putAll(userCouponService.countUser(userCoupon));

        return Result.success(count);
    }

    /**
     * 用户续租
     * 
     * @param member
     * @return
     * @throws IOException 
     */
    @PutMapping(value = "/addDayGoOn")
    @PreAuthorize("@ss.hasPermi('/member/update')")
    public Object addDayGoOn(@RequestBody MemberVo member) throws IOException {
        // 校验参数
        Result<?> paramResult = MemberParamVerify.addDayGoOn(member);
        // 验证不通过
        if (!paramResult.getCode().equals(TransErrorCode.SUCCESS.getCode())) {
            return paramResult;
        }

        // 参数初始化
        MemberVo mem = new MemberVo();
        mem.updateInit();
        mem.setNo(member.getNo());
        mem.setDayGoOn(member.getDayGoOn());
        // 修改信息
        return Result.success(memberServiceImpl.addDayGoOn(mem));
    }

    /**
     * 重置支付密码
     * 
     * @param id
     * @return
     * @throws SQLException
     * @throws IOException 
     */
    @PutMapping(value = "/rePayPwd/{id}")
    @PreAuthorize("@ss.login()")
    public Object rePayPwd(@PathVariable Integer id) throws SQLException, IOException {
        Member member = new Member();
        member.setId(id);
        member.setPayPwd(Constant.USER_PASSWORD_DEFAULT_ENCODE);
        member.updateInit();
        // 修改信息
        return Result.success(memberDaoImpl.updatePartialById(member));
    }

}